<template>
	<view class="update" v-if="uploadData">
		<view class="box">
			<image src="../../static/logo.png" mode="widthFix" class="logo"></image>
			<navigator url="../login/login">V {{ uploadData.versionName }}</navigator>
		</view>		
		<view class="progress-wrap">
			<progress :percent="percent" stroke-width="10" activeColor="#00A0E9"/>
			<view class="uni-flex">
				<view class="uni-flex-item">下载进度</view>
				<view>{{ percent }}%</view>
			</view>
		</view>
		<view class="block" v-if="uploadData.versionInfo">
			<rich-text :nodes="uploadData.versionInfo"></rich-text>
			<!-- <view>V2.0版本修改内容</view>
			<view>1.修改部分bug</view>
			<view>增加开机启动项</view> -->
		</view>
		<button type="primary" size="default" @click="cancel" v-show="!uploadData.forceUpdate">取消升级</button>
	</view>
</template>

<script>
	var _self
	export default {
		data() {
			return {
				percent:0,
				uploadData:'',
				lastProgressValue:0,
				dtask:''
			}
		},
		onLoad(option) {
			_self = this
			this.uploadData = JSON.parse(option.data) 
			this.downloader()
		},
		methods: {
			downloader(){
				var data = this.uploadData
				
				this.dtask = plus.downloader.createDownload(data.downloadUrl, {
				    filename: "_doc/update/"
				}, function(d, status) {
					if(status==200){
						var fileSaveUrl = plus.io.convertLocalFileSystemURL(d.filename);
						console.log(fileSaveUrl)
						debugger
						plus.runtime.install(d.filename, {}, function() {
							uni.showModal({
								title: '提示',
								content: '应用资源更新完成',
								showCancel: false,
								confirmText: '确定',
								success: res => {
									if(res.confirm){
										plus.runtime.restart();
									}
								}
							});
							
						}, function(e) {
						    plus.nativeUI.alert("安装文件失败[" + e.code + "]：" + e.message);
						});
						// plus.runtime.openFile(d.filename);
					}else{	
						//下载失败
						plus.downloader.clear();        //清除下载任务
					}
				})
				this.dtask.start()
				this.dtask.addEventListener("statechanged",(task, status)=>{
					switch (task.state) {
						case 1: // 开始
							console.log("准备下载...")
						    break;
						case 2: // 已连接到服务器  
							console.log("开始下载...")
							break;
					    case 3:
					        const progress = parseInt(task.downloadedSize / task.totalSize * 100);
					        if(progress - _self.lastProgressValue >= 2){
					            _self.lastProgressValue = progress;
								_self.percent = progress
					        }
					        break;
					}
				},false)
			},
			cancel(){
				this.dtask && this.dtask.abort();
				uni.showToast({
					title: "已取消下载",
					icon:"none"
				});
				uni.switchTab({
					url:"../mine/mine"
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.update {
	padding: 0 $uni-spacing-row-lg;
}
.box {
	padding: 160upx 0 50upx;
	margin-bottom: $uni-spacing-col-lg;
	text-align: center;
	uni-image {
		margin-bottom: $uni-spacing-col-base;
		width: 160upx;
		height: 160upx;
	}
	
}
.progress-wrap {
	margin-bottom: 80upx;
	/deep/ .uni-progress-bar {
		border-radius: 10px;
		overflow: hidden;
	}
	.uni-flex {
		margin-top: $uni-spacing-col-sm;
		color: $uni-text-color-grey;
		font-size: 28upx;
	}
}
.block {
	padding: 30upx;
	margin-bottom: $uni-spacing-col-base*2;
	background-color: #eee;
	border-radius: 20upx;
	view {
		margin-bottom: 15upx;
		line-height: 1.5;
		font-size: 28upx;
		color: $uni-text-color-grey;
	}
}
uni-button {
	padding: 0;
	line-height: 90upx;
	margin: 0 auto;
	width: 30%;
	font-size: 28upx;
	border-color: #ddd;
}

</style>
